﻿/*
 * ──────────────────────────────────────────────────────────────
 *　描   述：
 *　作   者：王浩                                              
 *　版   本：1.0
 *　创建时间：2022/9/8 11:09:21                             
 * ──────────────────────────────────────────────────────────────
 *　命名空间: AllProAutoUpData                                   
 *　类   名: UpServer                                         
 * ──────────────────────────────────────────────────────────────
 */
using HPSocket.Tcp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace AllProAutoUpData.Server
{
    /// <summary>
    /// 更新服务器
    /// </summary>
    public class UpServer
    {
        /// <summary>
        /// 惠普服务器
        /// </summary>
        TcpServer HPServer { get; set; }
        /// <summary>
        /// 端口
        /// </summary>
        ushort Port { get; set; }
        /// <summary>
        /// 客户端列表
        /// </summary>
        public Dictionary<string, IntPtr> Clients = new Dictionary<string, IntPtr>();
        public List<string> Message = new List<string>();
        public event Action<byte[], IntPtr> MessReceive;
        /// <summary>
        /// 启动
        /// </summary>
        public void Start(ushort port)
        {
            if (HPServer != null)
            {
                return;
            }
            Port = port;
            HPServer = new TcpServer();
            HPServer.Port = Port;
            HPServer.OnAccept += HPServer_OnAccept;
            HPServer.OnClose += HPServer_OnClose;
            HPServer.OnReceive += HPServer_OnReceive;
            HPServer.OnShutdown += HPServer_OnShutdown;
            HPServer.Start();
        }
        public void Close()
        {
            if (HPServer != null)
            {
                HPServer.Stop();
            }
            HPServer = null;
        }
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="intPtr"></param>
        /// <param name="data"></param>
        public void Send(IntPtr intPtr, byte[] data)
        {
           var dst= HPServer.Send(intPtr, data, data.Length);
            AddMess("发送成功"+ dst);
        }
        public void AddMess(string str)
        {
            Message.Add(DateTime.Now+ str);
            if (Message.Count>10)
            {
                Message.RemoveAt(0);
            }
        }
        /// <summary>
        /// 服务关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private HPSocket.HandleResult HPServer_OnShutdown(HPSocket.IServer sender)
        {
            AddMess("服务关闭");
            Clients.Clear();
            return HPSocket.HandleResult.Ok;
        }
        /// <summary>
        /// 数据到达
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="connId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private HPSocket.HandleResult HPServer_OnReceive(HPSocket.IServer sender, IntPtr connId, byte[] data)
        {
            AddMess("数据到达");
            MessReceive?.Invoke(data,connId);
            
            return HPSocket.HandleResult.Ok;
        }
        /// <summary>
        /// 链接关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="connId"></param>
        /// <param name="socketOperation"></param>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private HPSocket.HandleResult HPServer_OnClose(HPSocket.IServer sender, IntPtr connId, HPSocket.SocketOperation socketOperation, int errorCode)
        {
            AddMess("链接关闭");
            Clients.Remove(sender.Address);
            return HPSocket.HandleResult.Ok;
        }
        /// <summary>
        /// 链接到达
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="connId"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private HPSocket.HandleResult HPServer_OnAccept(HPSocket.IServer sender, IntPtr connId, IntPtr client)
        {
            AddMess("链接到达");
            Clients.Add(sender.Address, connId);
            return HPSocket.HandleResult.Ok;
        }
    }
}
